package com.web.daonila.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.web.daonila.service.bussiness.MessageRespBase;
import com.web.daonila.util.MenuConstants;
import com.web.daonila.util.MessageUtil;
import com.web.util.Constants;


/**
 * 核心服务类
 * 
 * @author liu.feng
 * @date 2013-05-20
 */
@Service
public class CoreService {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MessageRespBase textMessageResp;
	@Autowired
	private MessageRespBase articleMessageResp;
	@Autowired
	private MessageRespBase meetGoodBookResp;
	
	public String processRequest(HttpServletRequest request) {
		String respMessage = null;
		String respContent = "请求处理异常，请稍候尝试！";
		
		try {
			// 默认返回的文本消息内容

			// xml请求解析
			Map<String, String> requestMap = MessageUtil.parseXml(request);

			String msgType = requestMap.get("MsgType");


			// 文本消息
			if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
				respContent = textMessageResp.start(requestMap);
				logger.info("TextMessageResp.start():{requestMap={}}", new Object[]{respContent});
			}
			// 图片消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				respContent = articleMessageResp.start(requestMap);
				logger.info("TextMessageResp.start():{requestMap={}}", new Object[]{respContent});
			}
			// 地理位置消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				respContent = "您发送的是地理位置消息！";
			}
			// 链接消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				respContent = "您发送的是链接消息！";
			}
			// 音频消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				respContent = "您发送的是音频消息！";
			}
			// 事件推送
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				// 事件类型
				String eventType = requestMap.get("Event");
				// 订阅
				if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
					respContent = "谢谢您的关注！";
				}
				// 取消订阅
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
					// TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息
				}
				// 自定义菜单点击事件
				else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
					// 事件KEY值，与创建自定义菜单时指定的KEY值对应
					String eventKey = requestMap.get("EventKey");

					if (eventKey.equals(MenuConstants.MEET_ALL_READING)) {
						respContent = meetGoodBookResp.start(requestMap);
					} else if (eventKey.equals("12")) {
						respContent = "公交查询菜单项被点击！";
					} else if (eventKey.equals("13")) {
						respContent = "周边搜索菜单项被点击！";
					} else if (eventKey.equals("14")) {
						respContent = "历史上的今天菜单项被点击！";
					} else if (eventKey.equals("21")) {
						respContent = "歌曲点播菜单项被点击！";
					} else if (eventKey.equals("22")) {
						respContent = "经典游戏菜单项被点击！";
					} else if (eventKey.equals("23")) {
						respContent = "美女电台菜单项被点击！";
					} else if (eventKey.equals("24")) {
						respContent = "人脸识别菜单项被点击！";
					} else if (eventKey.equals("25")) {
						respContent = "聊天唠嗑菜单项被点击！";
					} else if (eventKey.equals("31")) {
						respContent = "Q友圈菜单项被点击！";
					} else if (eventKey.equals("32")) {
						respContent = "电影排行榜菜单项被点击！";
					} else if (eventKey.equals("33")) {
						respContent = "幽默笑话菜单项被点击！";
					}
				}
			}
			respMessage = respContent;
		} catch (Exception e) {
			e.printStackTrace();
		}
		logger.info("TextMessageResp.start():{requestMap={}}", new Object[]{respMessage});
		return respMessage;
	}
}